Handle negative timestamps in from_timestamp - #3026
Conversation
|
I don't think you need a platform check — the issue is return (dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc) + dt.timedelta(seconds=value)).replace(tzinfo=None)Both of your new cases pass with it on Windows (py3.12): |
|
Nice. But I suppose this comes with a performance penalty, so I'd rather do it only when necessary. |
|
Good call, that keeps the fast path untouched. On Windows it's try:
return dt.datetime.fromtimestamp(value, tz=dt.timezone.utc).replace(tzinfo=None)
except OSError:
return (dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc) + dt.timedelta(seconds=value)).replace(tzinfo=None)Both new cases pass here on Windows (py3.12): |
|
It revealed a little more complicated as there are cases where This implies a slight perf regression on out-of-range values, since we get en It seems acceptable to penalize an error path for a wider feature coverage. The code looks ugly, but oh well. |
Closes #3019.
Replacement for #3020.
I rewrote #3020 in a less AI-verbose way.
Before submitting, I checked why we prevented negative timestamps in the first place and it seems it is because those are not supported on Windows. See e.g. https://stackoverflow.com/a/71915839.
This sucks.
Should we add a platform check to let this work on sane (non-MS) environments?